//
//  GSCommonFindPassResetController.m
//  GSLoginDemo
//
//  Created by guoshikeji_a on 2018/7/30.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "GSCommonFindPassResetController.h"

#import "GSCommonFindPassResetView.h"

#import "GSCommonLoginPointAlertLabelView.h"

#import "GSCommonLoginSuccessController.h"

#import "NSString+RegexCategory.h"

//request
#import "BCFindPasswordResetRequest.h"

@interface GSCommonFindPassResetController ()

@property (nonatomic, strong) GSCommonFindPassResetView *passResetView;

@end

@implementation GSCommonFindPassResetController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.largeTitleLabel.text = GSLoginLocalized(@"Reset Password");
    [self setupSubviews];
    //
    __weak typeof(self) wself = self;
    self.passResetView.resetComplete = ^(BOOL valid) {
        if (wself.doneButton.enabled!=valid) {
            wself.doneButton.enabled = valid;
        }
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupSubviews {
    self.passResetView = [GSCommonFindPassResetView new];
    [self.view addSubview:self.passResetView];
    [self.passResetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.largeTitleLabel.mas_bottom).offset(15.0);
        make.height.mas_equalTo(110.0);
    }];
    //
    CGFloat leftPadding = 20.0;
    CGFloat rightPadding = 12.5;
    GSCommonLoginPointAlertLabelView* lableView = [GSCommonLoginPointAlertLabelView new];
    [self.view addSubview:lableView];
    [lableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.passResetView.mas_bottom).offset(19.0);
        make.left.mas_equalTo(leftPadding);
        make.width.mas_equalTo(self.view.frame.size.width-leftPadding-rightPadding);
    }];
    lableView.alertString = GSLoginLocalized(@"Must be 6 to 20 English letters, Numbers, or characters(except space), and letters, Numbers and punctuation contains at least two.");
    //
    [self.view addSubview:self.doneButton];
    [self.doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lableView.mas_bottom).offset(60.0);
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo([GSCommonLoginButton defaultSize]);
    }];
    self.doneButton.enabled = false;
}


#pragma mark -
- (void)doneButtonDidClickedAction {
    [self.view endEditing:true];
    if ([self checkPassword]) {
        //更新数据
        [self requestPasswordBackAction];
    }
}
- (BOOL)checkPassword {
    //先判断两次密码是否一致 然后正则判断密码是否符合要求
    if (![self.passResetView.password isEqualToString:self.passResetView.rePassword]) {
        NSString *info = GSLoginLocalized(@"twice password not the same");
        [QMUITips showInfo:info inView:self.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:info]];
        return false;
    }
    if (![NSString gsLoginPasswordRegex:self.passResetView.password]) {
        NSString *errorTitleMsg = GSLoginLocalized(@"password format error");
        [QMUITips showError:errorTitleMsg inView:self.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:errorTitleMsg]];
        return false;
    }
    return true;
}

//
- (void)requestPasswordBackAction {
    [self.view showLoginToast];
    BCFindPasswordResetRequest *request = [BCFindPasswordResetRequest new];
    request.password = self.passResetView.password;
    request.encryption = [GSCommonLoginDataManager sharedData].findPassModel.encryption;
    request.uuid = [GeneralParameterManager sharedGeneral].uuid;
    //
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself.view hideLoginToast];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        BCFindPasswordResetResponse *resp = result;
        if (resp.data) {
            [[BCUserManager sharedUser] userLoginSuccessUpdateUserLoginData:resp.data];
        }
        [wself resetPasswordSuccessToHomeAction];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error&&wself) {
            NSString *msg = error.domain;
            [QMUITips showError:msg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:msg]];
        }
    }];
    [request requestServer];
}


#pragma mark - 重置成功后返回首页
- (void)resetPasswordSuccessToHomeAction {
    __weak typeof(self) wself = self;
    [GSCommonLoginSuccessController showFromController:self succssType:GSCommonLoginSuccessTypeFindPasswordSuccess complete:^{
        [wself.navigationController dismissViewControllerAnimated:true completion:^{
            [wself.navigationController popToRootViewControllerAnimated:true];
            [[NSNotificationCenter defaultCenter] postNotificationName:UserCommonLoginSuccessNotification object:nil];
        }];
    }];
}

@end
